package com.diqizu.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.diqizu.entity.NewsType;
import com.diqizu.utli.DBManager;
import com.diqizu.utli.QueryDataResult;

public class NewsTypeDaoImpl {
	public ArrayList<NewsType> getAllList() {
		String sql = "select * from newstype";
		System.out.println("执行的sql语句是:" + sql);
		QueryDataResult execQuery = DBManager.execQuery(sql);
		ArrayList<NewsType> list = new ArrayList<NewsType>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					NewsType e = new NewsType(rs.getInt("id"), rs.getString("name"));
					list.add(e);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println("获取的数据是：" + list);
		return list;
	}

	/**
	 * 增加方法
	 * 
	 * @param Banner
	 * @return
	 */
	public boolean insertnewstype(NewsType type) {
		String sql = "insert into newstype (name) values('" + type.getName() + "')";
		System.out.println(sql);
		boolean i = DBManager.execUpdate(sql);
		if (i == true) {
			return true;
		}
		return false;
	}

	/**
	 * 修改方法
	 * 
	 * @param Banner
	 * @return
	 */
	public boolean updatenewstype(NewsType type) {
		String sql = "update newstype set name = '" + type.getName() + "' where id = " + type.getId();
		System.out.println(sql);
		boolean i = DBManager.execUpdate(sql);
		if (i == true) {
			return true;
		}
		return false;
	}

	/**
	 * 删除方法
	 * 
	 * @param Banner
	 * @return
	 */
	public boolean delnewstype(int id) {
		String sql = "delete from newstype where id =" + id;
		System.out.println(sql);
		boolean i = DBManager.execUpdate(sql);
		if (i == true) {
			return true;
		}
		return false;
	}

}
